<template>
  <div>
    <!-- 搜索栏区域 -->
    <el-card class="SearchCard" body-style="padding:0px">
      <el-row>
        <el-col :span="2">
          <div class="SearchTitle">条件搜索栏</div>
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-col :span="10">
              <div class="SearchLable">搜索关键字</div>
            </el-col>

            <el-col :span="14">
              <div class="SearchLable">
                <el-input placeholder="请输入内容" v-model="queryInfo.srm" clearable @clear="clearEvent"></el-input>
              </div>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium" type="primary" @click="getSearchList()">搜索</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="TableCard" :body-style="{ padding: '0px' }">
      <div>
        <el-table
          :data="tableData"
          border
          height="550"
          :header-cell-style="{ 'text-align':'center',background: '#3F9DFD', color: '#FFF' }"
          :cell-style="{'text-align':'center'}"
          size="medium"
          stripe
          style="width: 100%;font-size:12px"
        >
          <el-table-column prop="drugCode" label="药品编码" width="100"></el-table-column>
          <el-table-column prop="name" label="操作" width="120">
            <template slot-scope="scope">
              <!-- v-if="scope.row.indicaDrugId>0" -->
              <el-button
                type="text"
                size="mini"
                @click="showRuleAll(scope.row.drugName,scope.row.drugCode,scope.row.indicaDrugId)"
              >查看</el-button>
              <el-button
                type="text"
                size="mini"
                @click="showAddRule(scope.row.drugName, scope.row.drugCode)"
              >新增</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="drugName" label="药品名称"></el-table-column>

          <!-- <el-table-column prop="cbrand" label="商品名"></el-table-column> -->
          <el-table-column label="药品类型">
            <template slot-scope="scope">
              <div v-if="scope.row.nameType==8">西药</div>
              <div v-else-if="scope.row.nameType==2">草药</div>
              <div v-else-if="scope.row.nameType==6">中成药</div>
            </template>
          </el-table-column>

          <el-table-column prop="compName" label="药品生产厂家" width="220"></el-table-column>
          <el-table-column prop="drugSpec" label="规格" width="220"></el-table-column>
          <el-table-column prop="approvalCode" label="批准文号"></el-table-column>
          <el-table-column prop="doseUnit" label="单位"></el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[20, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
          background
        ></el-pagination>
      </div>
    </el-card>

    <!-- 新增规则 -->
    <el-dialog
      :title="'新增适应症规则'+'—'+this.addRuleData.drugName"
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="handleClose"
    >
      <div class="addRuleAdverse">
        <el-form :inline="true" :model="addRuleData" class="demo-form-inline" label-width="100px">
          <el-form-item label="疾病编码">
            <el-input v-model="addRuleData.icdName" size="medium">
              <el-button slot="append" @click="showDisease(1)">选择</el-button>
            </el-input>
          </el-form-item>
          <el-form-item label="描述" class="abstractDesc">
            <el-input
              type="textarea"
              :autosize="{ minRows: 5, maxRows: 5}"
              v-model="addRuleData.abstractDesc"
              size="medium"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addRule()">新 增</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="疾病诊断"
      :visible.sync="DiseaseVisible"
      class="editDialog"
      width="30%"
      :before-close="handleClose"
    >
      <div style="width:100%;height:527px">
        <div class="addInput">
          <el-input v-model="DiseaseData.srm" size="small" placeholder="请输入内容"></el-input>
          <el-button style="margin-right:8px" type="primary" size="small" @click="SearchDrug()">搜索</el-button>
        </div>
        <el-table
          :data="DiseaseData.tableData"
          stripe
          border
          height="400"
          style="width: 100%;font-size: 10px"
          :header-cell-style="{'text-align':'center'}"
          :row-style="{height:'40px'}"
          :cell-style="{'text-align':'center',padding:'0px'}"
        >
          <el-table-column label width="35">
            <template slot-scope="scope">
              <el-radio :label="scope.row" v-model="DiseaseData.radioId">&nbsp;</el-radio>
            </template>
          </el-table-column>

          <el-table-column prop="zddm" width="150" label="诊断代码"></el-table-column>
          <el-table-column prop="zdmc" label="诊断名称"></el-table-column>
        </el-table>
        <!-- <el-pagination background layout="prev, pager, next" :total="1000"></el-pagination> -->
        <div class="dialogBlock">
          <el-pagination
            @size-change="handleSizeChange3"
            @current-change="handleCurrentChange3"
            current-page.sync="1"
            :page-size="100"
            layout="total, prev, pager, next"
            :total="DiseaseData.total"
          ></el-pagination>
        </div>
        <span slot="footer" class="dialog-footer">
          <div class="SortFoot">
            <el-button
              style="margin-right:8px"
              type="primary"
              size="small"
              @click="SelectDisease()"
            >确定</el-button>
            <el-button style="margin-right:8px" @click="RouteVisible = false" size="small">取消</el-button>
          </div>
        </span>
      </div>
    </el-dialog>
    <!-- 所有规则 -->
    <el-dialog
      :title="'所有适应症规则'+'—'+this.RuleAllData.drugName"
      :visible.sync="RuleAllVisible"
      class="editDialog"
      width="55%"
      stripe
      :before-close="handleClose"
    >
      <div>
        <el-card style="height:530px">
          <el-table
            :data="RuleAllData.RuleAllTableList"
            :header-cell-style="{'text-align':'center'}"
            :cell-style="{'text-align':'center',padding:'0px'}"
            border
            stripe
            height="450"
            style="width: 100%"
          >
            <el-table-column type="index" label="序号" width="50" fixed></el-table-column>
            <el-table-column label="操作" width="100" fixed>
              <template slot-scope="scope">
                <el-button
                  type="text"
                  style="margin-right:12px"
                  @click="showRuleOne(scope.row.id)"
                >查看</el-button>
                <el-button type="text" @click="open(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>

            <el-table-column prop="drugId" label="药物禁忌症ID" width="130"></el-table-column>
            <el-table-column prop="diseaseId" label="疾病ID" width="100"></el-table-column>

            <el-table-column prop="icdCode" label="疾病编码" width="85"></el-table-column>
            <el-table-column prop="routeIds" label="给药途径id" width="150"></el-table-column>
            <el-table-column prop="routeType" label="给药类型" width="110"></el-table-column>
            <el-table-column prop="routeDesc" label="给药描述" width="85"></el-table-column>
            <el-table-column prop="slCode" label="警示级别" width="150"></el-table-column>
            <el-table-column prop="warning" label="警示内容" width="110"></el-table-column>
            <el-table-column prop="content" label="内容" width="130"></el-table-column>
          </el-table>
          <el-pagination
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"
            :current-page="1"
            :page-sizes="[20, 30, 50]"
            :page-size="100"
            layout="total, sizes, prev, pager, next, jumper"
            :total="RuleAllData.total"
          ></el-pagination>
        </el-card>
      </div>
    </el-dialog>
    <!-- 查看规则 -->
    <el-dialog
      title="肝功能规则"
      :visible.sync="showRuleOneVisible"
      width="35%"
      :before-close="handleClose"
    >
      <div class="addRuleAdverse">
        <el-form
          :inline="true"
          :model="showRuleOneData"
          class="demo-form-inline"
          label-width="150px"
        >
          <el-form-item label="给药途径ID">
            <el-input v-model="showRuleOneData.routeId" size="medium">
              <el-button slot="append">选择</el-button>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateRuleOne()">更 新</el-button>
        <el-button @click="showRuleOneVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  // getRuleListIndica,
  // addRuleInfoIndica,
  // showRuleAllIndica,
  // showRuleOneIndica,
  // updateRuleOneIndica,
  // deleteRuleIndica
  getRuleListIndica,
  addRuleInfoIndica,
  showRuleAllIndica,
  showRuleOneIndica,
  updateRuleOneIndica,
  deleteRuleIndica,
} from "@/api/rad";
export default {
  data() {
    return {
      queryInfo: {
        srm: "",
        page: 1,
        pageSize: 20,
      },
      total: 0,
      tableData: [],
      dialogVisible: false,
      DiseaseVisible: false,
      DiseaseData: {
        srm: "",
        page: 1,
        pageSize: 20,
        load: 0,
        total: 0,
        tableData: [],
        radioId: [],
      },
      addRuleData: {
        drugCode: "",
        icdCode: "",
        icdName: "",
        abstractDesc: "",
      },
      RuleAllVisible: false,
      RuleAllData: {
        drugCode: "",
        drugName: "",
        rendosageDrugId: "",
        page: 1,
        pageSize: 20,
        total: 0,
        RuleAllTableList: [],
      },
      showRuleOneVisible: false,
      showRuleOneData: {
        id: "",
        drugId: "",
        routeId: "",
        // routeName: "",
        hepLabel: "",
        ageLow: "",
        unequalLow: "",
        ageLowUnit: "",
        ageHigh: "",
        unequalHigh: "",
        ageHighUnit: "",
        weighLow: "",
        weighHigh: "",
        calculateLabel: "",
        doseEachLow: "",
        doseEachLowUnit: "",
        doseEachHigh: "",
        doseEachHighUnit: "",
        doseDayLow: "",
        doseDayLowUnit: "",
        doseDayHigh: "",
        doseDayHighUnit: "",
        freQuencyLow: "",
        freQuencyLowDay: "",
        freQuencyHigh: "",
        freQuencyHighDay: "0",
        abstractDesc: "",
      },
      DiseaseData: {
        srm: "",
        page: "",
        pageSize: "",
        load: 0,
        total: 0,
        tableData: [],
        radioId: [],
      },
    };
  },
  created: function () {
    this.getRuleListIndica();
  },
  methods: {
    handleClose(done) {
      done();
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      console.log(this.queryInfo);
      this.getRuleListIndica();
    },

    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      console.log(this.queryInfo);
      this.getRuleListIndica();
    },

    handleSizeChange2(newSize) {
      this.RuleAllData.pageSize = newSize;
      this.showRuleAllIndica();
    },
    handleCurrentChange2(newPage) {
      this.RuleAllData.page = newPage;
      this.showRuleAllIndica();
    },
    handleSizeChange3(newSize) {
      this.DiseaseData.pageSize = newSize;
      this.showRuleDiseaseAge();
    },
    handleCurrentChange3(newPage) {
      this.DiseaseData.page = newPage;
      this.showRuleDiseaseAge();
    },
    async getRuleListIndica() {
      let res = await this.$rad.getRuleListIndica({
        srm: this.queryInfo.srm,
        page: this.queryInfo.page,
        pageSize: this.queryInfo.pageSize,
      });
      this.tableData = res.rows;
      this.total = res.total;
      console.log(res.rows);
    },
    getSearchList() {
      this.getRuleListIndica();
    },
    clearEvent() {},
    showAddRule(drugName, drugCode) {
      this.addRuleData.drugName = drugName;
      this.addRuleData.drugCode = drugCode;
      this.dialogVisible = true;
    },
    showDisease(load) {
      this.DiseaseData.load = load;
      this.DiseaseVisible = true;
      this.showRuleDiseaseAge();
    },
    async showRuleDiseaseAge() {
      let res = await this.$rad.showRuleDiseaseAge({
        srm: this.DiseaseData.srm,
        page: this.DiseaseData.page,
        pageSize: this.DiseaseData.pageSize,
      });
      console.log(res);
      this.DiseaseData.tableData = res.rows;
      this.DiseaseData.total = res.total;
    },
    SelectDisease() {
      if (this.DiseaseData.radioId.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择疾病诊断",
        });
      } else {
        if (this.DiseaseData.load == 1) {
          console.log(this.DiseaseData.radioId);
          this.addRuleData.icdName = this.DiseaseData.radioId.zdmc;
          this.addRuleData.icdCode = this.DiseaseData.radioId.zddm;
        } else if (this.DiseaseData.load == 2) {
          // console.log
          this.showRuleOneData.icdName = this.DiseaseData.radioId.zdmc;
          this.showRuleOneData.icdCodes = this.DiseaseData.radioId.zddm;
        }

        this.DiseaseVisible = false;
      }
    },
    async addRule() {
      let res = await this.$rad.addRuleInfoIndica({
        drugCode: this.addRuleData.drugCode,
        icdCode: this.addRuleData.icdCode,
        abstractDesc: this.addRuleData.abstractDesc,
      });
      console.log(res);
      if (res.code == 1) {
        this.dialogVisible = false;
        this.getRuleListIndica();
        this.clearAddRuleData();
        return this.$message({
          type: "success",
          message: res.msg,
        });
      } else {
        return this.$message({
          type: "error",
          message: res.msg,
        });
      }
    },
    showRuleAll(drugName, drugCode, rendosageDrugId) {
      this.RuleAllData.drugCode = drugCode;
      this.RuleAllData.drugName = drugName;
      this.RuleAllData.rendosageDrugId = rendosageDrugId;
      this.RuleAllVisible = true;
      this.showRuleAllIndica();
    },
    async showRuleAllIndica() {
      let res = await this.$rad.showRuleAllIndica({
        drugId: this.RuleAllData.rendosageDrugId,
        page: this.RuleAllData.page,
        pageSize: this.RuleAllData.pageSize,
      });
      this.RuleAllData.RuleAllTableList = res.rows;
      this.RuleAllData.total = res.total;
      console.log(this.RuleAllData.RuleAllTableList);
    },
    showRuleOne(id) {
      this.showRuleOneData.id = id;
      // console.log(id);
      this.showRuleOneVisible = true;
      this.showRuleOneIndica();
    },
    async showRuleOneIndica() {
      let res = await this.$rad.showRuleOneIndica({
        id: this.showRuleOneData.id,
      });
      console.log(res);

      (this.showRuleOneData.drugId = res.data.drugId),
        (this.showRuleOneData.routeId = res.data.routeId),
        (this.showRuleOneData.hepLabel = res.data.renLabel),
        (this.showRuleOneData.ageLow = res.data.ageLow),
        (this.showRuleOneData.unequalLow = res.data.unequalLow),
        (this.showRuleOneData.ageLowUnit = res.data.ageLowUnit),
        (this.showRuleOneData.ageHigh = res.data.ageHigh),
        (this.showRuleOneData.unequalHigh = res.data.unequalHigh),
        (this.showRuleOneData.ageHighUnit = res.data.ageHighUnit),
        (this.showRuleOneData.weighLow = res.data.weighLow),
        (this.showRuleOneData.weighHigh = res.data.weighHigh),
        (this.showRuleOneData.calculateLabel = res.data.calculateLabel),
        (this.showRuleOneData.doseEachLow = res.data.doseEachLow),
        (this.showRuleOneData.doseEachLowUnit = res.data.doseEachLowUnit),
        (this.showRuleOneData.doseEachHigh = res.data.doseEachHigh),
        (this.showRuleOneData.doseEachHighUnit = res.data.doseEachHighUnit),
        (this.showRuleOneData.doseDayLow = res.data.doseDayLow),
        (this.showRuleOneData.doseDayLowUnit = res.data.doseDayLowUnit),
        (this.showRuleOneData.doseDayHigh = res.data.doseDayHigh),
        (this.showRuleOneData.doseDayHighUnit = res.data.doseDayHighUnit),
        (this.showRuleOneData.freQuencyLow = res.data.freQuencyLow),
        (this.showRuleOneData.freQuencyLowDay = res.data.freQuencyLowDay),
        (this.showRuleOneData.freQuencyHigh = res.data.freQuencyHigh),
        (this.showRuleOneData.freQuencyHighDay = res.data.freQuencyHighDay),
        (this.showRuleOneData.abstractDesc = res.data.abstractDesc);
    },
    async updateRuleOne() {
      // console.log(this.showRuleOneData);
      let res = await this.$rad.updateRuleOneIndica({
        id: this.showRuleOneData.id,
        strouteId: this.showRuleOneData.routeId,
        // routeName: this.showRuleOneData.,
        renLabel: this.showRuleOneData.hepLabel,
        ageLow: this.showRuleOneData.ageLow,
        unequalLow: this.showRuleOneData.unequalLow,
        ageLowUnit: this.showRuleOneData.ageLowUnit,
        ageHigh: this.showRuleOneData.ageHigh,
        unequalHigh: this.showRuleOneData.unequalHigh,
        ageHighUnit: this.showRuleOneData.ageHighUnit,
        weighLow: this.showRuleOneData.weighLow,
        weighHigh: this.showRuleOneData.weighHigh,
        calculateLabel: this.showRuleOneData.calculateLabel,
        doseEachLow: this.showRuleOneData.doseEachLow,
        doseEachLowUnit: this.showRuleOneData.doseEachLowUnit,
        doseEachHigh: this.showRuleOneData.doseEachHigh,
        doseEachHighUnit: this.showRuleOneData.doseEachHighUnit,
        doseDayLow: this.showRuleOneData.doseDayLow,
        doseDayLowUnit: this.showRuleOneData.doseDayLowUnit,
        doseDayHigh: this.showRuleOneData.doseDayHigh,
        doseDayHighUnit: this.showRuleOneData.doseDayHighUnit,
        freQuencyLow: this.showRuleOneData.freQuencyLow,
        freQuencyLowDay: this.showRuleOneData.freQuencyLowDay,
        freQuencyHigh: this.showRuleOneData.freQuencyHigh,
        freQuencyHighDay: this.showRuleOneData.freQuencyHighDay,
        abstractDesc: this.showRuleOneData.abstractDesc,
      });
      if (res.code == 1) {
        this.showRuleOneVisible = false;
        this.showRuleAllIndica();
        return this.$message({
          type: "success",
          message: res.msg,
        });
      } else {
        return this.$message({
          type: "success",
          $message: res.msg,
        });
      }
    },
    open(id) {
      this.$confirm("此操作将永久删除该规则, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteRuleIndica(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async deleteRuleIndica(id) {
      let res = await this.$rad.deleteRuleIndica({
        id: id,
        drugId: this.RuleAllData.rendosageDrugId,
      });
      console.log(res);
      if (res.code == 1) {
        this.showRuleAllIndica();
        this.getRuleListIndica();
        return this.$message({
          type: "success",
          message: res.msg,
        });
      } else {
        return this.$message({
          type: "success",
          $message: res.msg,
        });
      }
    },
    clearAddRuleData() {},
  },
};
</script>
<style scoped>
.SearchCard {
  height: 50px;
  font-size: 12px;
}
.SearchTitle {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid #c2c2c2;
}
.SearchLable {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.TableCard {
  margin-left: 3px;
  margin-top: 2px;
}
.block {
  text-align: center;
  margin-top: 12px;
  margin-bottom: 12px;
}
.editDialog >>> .el-dialog__header {
  margin-bottom: 0px;
}
.editDialog >>> .el-dialog__body {
  padding-left: 0px;
  padding-right: 0px;
}
.addInput {
  height: 45px;
  display: flex;
  align-items: center;
}
.SortFoot {
  height: 50px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.addInput >>> .el-input {
  width: 40%;
  margin-left: 8px;
}
.addInput >>> .el-button {
  margin-left: 8px;
}
.dialogBlock {
  margin-top: 0px;
  margin-bottom: 0px;
}
.abstractDesc {
  width: 100%;
}
.abstractDesc >>> .el-form-item__content {
  width: 60%;
}
</style>